<!--
 * @Description: 事件
 * @Author: charles
 * @Date: 2021-11-09 09:22:10
 * @LastEditors: charles
 * @LastEditTime: 2021-11-10 10:36:59
-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>dom引用 ref</title>
  <script src="https://cdn.bootcdn.net/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
  <script src="https://cdn.bootcdn.net/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
  <script src="https://cdn.bootcss.com/babel-standalone/6.26.0/babel.min.js"></script>
  <script type="text/javascript" src="https://unpkg.com/@antv/g2plot@latest/dist/g2plot.min.js"></script>

</head>
<body>
  <div id="app"></div>

  <script type="text/babel">
    // let {Component,useEffect,useState} = React;
    
   
    class App extends React.Component {
      render(){
        return (
          <div>
            <h2>dom引用</h2>  
            <Chart/>
            <ChartLine/>
          </div>
        )
      }
    }

    class Chart extends React.Component {
      constructor(props){
        super(props)
        this.container = React.createRef();
      }
      componentDidMount(){
        const { Line } = G2Plot;
        const line = new Line(this.container.current, {
          data:[
            { year: '1991', value: 3 },
            { year: '1992', value: 4 },
            { year: '1993', value: 3.5 },
          ],
          xField: 'year',
          yField: 'value',
        });
        line.render();
      }

      render(){
        return (
          <div>
            <h3>chart</h3>  
            <div ref={this.container}></div>
          </div>
        )
      }
    }

    function ChartLine(){
      const container = React.createRef()
      React.useEffect(()=>{
        const { Line } = G2Plot;
        const line = new Line(container.current, {
          data:[
            { year: '1991', value: 3 },
            { year: '1992', value: 4 },
            { year: '1993', value: 3.5 },
          ],
          xField: 'year',
          yField: 'value',
        });
        line.render();

        return ()=>{
          line.destroy();
        }
      })

      return (
        <div>
          <h2>函数组件</h2>  
          <div ref={container}></div>
        </div>
      )
    }

    ReactDOM.render(<App/>,document.getElementById('app'))
  </script>
</body>
</html>